--
-- Author: yawei.zhu
-- Date: 2018-09-26 15:21:05
--
-- WorldBossRender
--

local NewRareInfoViewEx = require("app.newBag.views.NewRareInfoViewEx")

local ClassRef = app.mvc.createRenderClass(...)
local wordDescs = {L("worldboss_desc1"), L("worldboss_desc2"), L("worldboss_desc3")}
local boxName = {L("worldbossbronzebox"), L("worldbosssilverbox"), L("worldbossgoldbox"), L("worldbossdiamondbox")}
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.WorldBossRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    -- 全服击杀奖励
    -- 极速击杀奖励
    -- 累计击杀奖励

    local tops = {}
    for i = 1, 3 do
        tops[i] = self._ui["top_"..i]
    end
    self._tops = tops
    
    self._txtNotice = self._ui.text_1
    self._txtRank = self._ui.num
    self._tipsFlag = self._ui.tips
    self._ui.btn_rank:addClickEventListener(function()
        executeFunc(self._onClick)
    end)
    self._ui["icon/icon"]:addClickEventListener(function()
        if self._isOpen then
            self:showRareInfo()
        else
            if self._reward then --有奖励
                if self._data.bossInfo:getBoxFlag(self._data.index) then
                    --display.pushToast(L("已领取过宝箱"))
                else
                    app:sendMsg("BossModel", "reqOpenWorldBossBox", self._data.index)
                end
            else
                self:showRareInfo()
            end
        end
    end)
    self._ui.icon_lock:setTouchEnabled(false)
    self._ui["icon_lock/icon"]:setTouchEnabled(false)


    self:refresh()
end

function ClassRef:refresh()
    if not self._data then return end
    self:clearItem()
    local index = self._data.index
    local bossInfo = self._data.bossInfo

    self._ui["top_"..index]:setVisible(true)
    self._ui.text_1:setString(wordDescs[index])

    local num = self._ui.num
    if index == 1 then 
        num:setString(bossInfo.svrKilledCount)
        self._ui.btn_rank:setVisible(false)
    elseif index == 2 then 
        if bossInfo.speedKilledRank <= 0 then
            num:setString(L("bossRank_notOnTheList"))
        else
            num:setString(bossInfo.speedKilledRank)
        end
    else
        if bossInfo.guildKilledRank <= 0 then
            num:setString(L("bossRank_notOnTheList"))
        else
            num:setString(bossInfo.guildKilledRank)
        end
    end

    --box
    self._worldBossStatusData = app:getInst("BossModel"):getWorldBossStatusData()
    if not self._worldBossStatusData then
        print("WorldBossRender WorldBossStatusData is nil.")
        return 
    end
    self._isOpen = self._worldBossStatusData.open or false
    local list
    if index == 1 then
        list = {index = 1, result = bossInfo.svrKilledCount}
    elseif index == 2 then
        list = {index = 2, result = bossInfo.speedKilledRank}
    elseif index == 3 then
        list = {index = 3, result = bossInfo.guildKilledRank}
    end
    self._reward, self._boxrank = GD:getWorldBossReward(list)
    if self._reward then
        local drops = UD:parseDropIDToBagItem(self._reward)
        self._ui["icon/icon"]:loadTexture(drops[1]:getIcon())
        self._ui["icon_lock/icon"]:loadTexture(drops[1]:getIcon())
    end
    if self._isOpen then
        self._ui["icon_lock"]:setVisible(true)
        self._ui["icon_lock/lock"]:setVisible(true)
        self._ui["icon_lock/over"]:setVisible(false)
    else
        if self._reward then
            if bossInfo:getBoxFlag(index) then --已领取宝箱
                self._ui["icon_lock"]:setVisible(true)
                self._ui["icon_lock/lock"]:setVisible(false)
                self._ui["icon_lock/over"]:setVisible(true)
            else
                self._ui["icon_lock"]:setVisible(false)
            end
        else
            self._ui["icon_lock"]:setVisible(true)
        end
    end
    --tips
    if index == 1 then
        self._ui["tips"]:setVisible(true)
        if self._boxrank then
            local str = boxName[self._boxrank]
            self._ui["tips"]:setString( string.format(L("worldbossunlocked"), str) )
        else
            local str = L("worldbossboxneed")
            self._ui["tips"]:setString( str )
        end
    else
        self._ui["tips"]:setVisible(false)
    end
end

function ClassRef:clearItem()
    for i = 1, 3 do
        local top = self._ui["top_"..i]
        top:setVisible(false)
    end
    self._ui.btn_rank:setVisible(true)
end

function ClassRef:showRareInfo()
    local data = {}

    if self._data.index == 1 then
        data[1] = {string = L("serverRewardDes1"), color = Enums.QualityColor2[1], font = 20}
        data[2] = {string = L("serverRewardDes2"), color = Enums.QualityColor2[2], font = 18}
        data[3] = {string = L("serverRewardDes3"), color = Enums.QualityColor2[3], font = 18}
    elseif self._data.index == 2 then
        data[1] = {string = L("firstRewardDes1"), color = Enums.QualityColor2[1], font = 20}
    elseif self._data.index == 3 then
        data[1] = {string = L("guildRewardDes1"), color = Enums.QualityColor2[1], font = 20}
        data[2] = {string = L("guildRewardDes2"), color = Enums.QualityColor2[2], font = 18}
    else 
        return
    end

    local rarePanel = NewRareInfoViewEx.create()
    rarePanel:setData( data )
    UIHandler:addPopupUi(rarePanel)

    -- 点击空白区域关闭
    display.nodeRemoveForNotHit(rarePanel, function()
        rarePanel:removeFromParent()
    end)

    local boundingBox = {}
    local contentSize = self._ui.icon:getContentSize()
    local worldPosTemp = self._ui.icon:convertToWorldSpace( cc.p(contentSize.width / 2, contentSize.height / 2) )
    boundingBox.x = worldPosTemp.x
    boundingBox.y = worldPosTemp.y
    boundingBox.width = 0
    boundingBox.height = 0
    display.adjustNodePos( boundingBox, rarePanel )

    -- dump(boundingBox)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
